﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SimpleWebChat
{
    public partial class Main : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            MessagesDataContext context = new MessagesDataContext();
            this.commentsRepeater.DataSource = context.Messages.OrderByDescending(m => m.PostDate).Take(100);
            DataBind();
        }

        protected void sendBtn_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.commentTxt.Text))
            {
                MessagesDataContext context = new MessagesDataContext();

                Message m = new Message()
                {
                    PostDate = DateTime.Now,
                    Message1 = this.commentTxt.Text
                };

                this.commentTxt.Text = "";
                context.Messages.InsertOnSubmit(m);
                context.SubmitChanges();
                this.commentTxt.Focus();
            }
        }
    }
}